﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xyz.Web.Reservation.CustomerService;
using Xyz.Web.Reservation.Models;
using Xyz.Web.Reservation.Repositories;

namespace Xyz.Web.Reservation.Controllers
{
    public class CustomerController : Controller
    {
        CustomerRepository cRep = new CustomerRepository();
        CityRepository cityRep = new CityRepository();
        CountryRepository countryRep = new CountryRepository();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Member()
        {
            LoadGenderListToSession();
            LoadCityListToSession();
            LoadCountryListToSession();
            MemberModel m = new MemberModel();
            return View(m);
        }

        [HttpPost]
        public ActionResult Member(FormCollection collection)
        {
            MemberModel m = new MemberModel();
            try
            {
                UpdateModel(m);
                MemberDTO md = (MemberDTO)Mapper.FromModel(m);
                cRep.CreateCustomer(md);

                return RedirectToAction("Login", "Auth", new { area = "" });
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(InvalidOperationException))
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
                return View(m);
            }
        }

        public ActionResult Edit()
        {
            if (Session["Customer"] == null)
            {
                return RedirectToAction("Member", "Customer", new { area = "" });
            }
            else
            {
                CustomerModel c = (CustomerModel)Session["Customer"];
                if (c.GetType() == typeof(MemberModel))
                {
                    LoadGenderListToSession();
                    LoadCityListToSession();
                    LoadCountryListToSession();
                    MemberModel m = (MemberModel)Session["Customer"];
                    return View(m);
                }
                else
                {
                    return RedirectToAction("Member", "Customer", new { area = "" });
                }
            }
        }

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            MemberModel m = new MemberModel();
            try
            {
                MemberModel tempM = (MemberModel)Session["Customer"];
                m.CustomerId = tempM.CustomerId;
                m.UserName = tempM.UserName;
                m.MemberId = tempM.MemberId;
                m.MemberPoints = tempM.MemberPoints;
                m.MemberPayments = tempM.MemberPayments;
                UpdateModel(m);
                MemberDTO md = (MemberDTO)Mapper.FromModel(m);
                cRep.UpdateCustomer(md);

                ViewData["SuccessMessage"] = "Member data are successfully saved.";
                return View(m);
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(InvalidOperationException))
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
                return View(m);
            }
        }

        private void LoadGenderListToSession()
        {
            List<SelectListItem> genderList = new List<SelectListItem>();
            SelectListItem itemF = new SelectListItem() { Value = "F", Text = "Female"  };
            SelectListItem itemM = new SelectListItem() { Value = "M", Text = "Male" };
            genderList.Add(itemF);
            genderList.Add(itemM);
            Session["GenderList"] = new SelectList(genderList, "Value", "Text"); ;
        }

        private void LoadCityListToSession()
        {
            Session["CityList"] = new SelectList(cityRep.GetAllCitites(), "CityCode", "CityName");
        }

        private void LoadCountryListToSession()
        {
            Session["CountryList"] = new SelectList(countryRep.GetAllCountries(), "CountryCode", "CountryName");
        }

    }
}
